package com.sheng.whyclass.work.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.toolbox.StringRequest;
import com.sheng.util.Constant;
import com.sheng.util.CustomToast;
import com.sheng.util.HttpProcessManager;
import com.sheng.util.VolleyManager;
import com.sheng.whyclass.R;
import com.sheng.whyclass.work.event.AddWorkEvent;

import de.greenrobot.event.EventBus;

public class AddWorkFragment extends Fragment implements View.OnClickListener{
    private View mRootView = null;
    private String work_title;
    private String work_content;

    private EditText addAnswerEdit;
    private EditText addWorkTitle;
    private Button submitBtn;




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }




    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_add_work, container, false);
        initView();
        return mRootView;
    }

    private void initView() {
//        StudentInfoData.DataBean data = LoginDataHelper.getInstance().getLoginUserInfo();
//        student_id =data.getId();
        addAnswerEdit = (EditText) mRootView.findViewById(R.id.addWorkContent);
        addWorkTitle = (EditText) mRootView.findViewById(R.id.addWorkTitle);
        submitBtn = (Button)mRootView.findViewById(R.id.submit_btn);
        submitBtn.setOnClickListener(this);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }


    public void onEventMainThread(AddWorkEvent event) {


        int code = event.code;


        if (code == Constant.CODE_FAIL) {
            CustomToast.showToast(getString(R.string.net_fail));

            return;
        } else {
           CustomToast.showToast(event.info);
            return;
        }

    }

    private void AddWork() {
        StringRequest request = HttpProcessManager.getInstance().AddWork(Constant.HOST_ADD_WORK, work_title,work_content);
        VolleyManager.getInstance(getActivity().getApplicationContext()).addToRequestQueue(request);
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id){
            case R.id.submit_btn:
                work_title = addWorkTitle.getText().toString().trim();
                work_content = addAnswerEdit.getText().toString().trim();
                if(TextUtils.isEmpty(work_title)||TextUtils.isEmpty(work_content)){
                    CustomToast.showToast(getString(R.string.para_null));
                    return;
                }

                AddWork();
                break;
        }
    }
}
